﻿/**
显示对象之位图
*/

class TestBitmap extends egret.DisplayObjectContainer {

    private textfield: egret.TextField;
    private leftBtn: egret.Shape;
    private rightBtn: egret.Shape;
    private bigText: egret.TextField;

    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.createGameScene, this);
    }

    private createGameScene(): void {
    
        var stageSize: egret.Point = new egret.Point(this.stage.stageWidth, this.stage.stageHeight);
        //背景
        var sky: egret.Bitmap = new egret.Bitmap();
        sky.texture = RES.getRes("bgImage");
        this.addChild(sky);
        sky.width = stageSize.x;
        sky.height = stageSize.y;
        //九宫格的使用
        var scaleBtn: egret.Bitmap = new egret.Bitmap();
        scaleBtn.texture = RES.getRes("scale9");
        console.log(scaleBtn.width, scaleBtn.height);
        scaleBtn.width =scaleBtn.width* 1.2;
        scaleBtn.height =scaleBtn.height* 1.5;
        console.log(scaleBtn.width, scaleBtn.height);
        scaleBtn.fillMode = egret.BitmapFillMode.SCALE;
        var rect: egret.Rectangle = new egret.Rectangle(10, 10, scaleBtn.width - 20, scaleBtn.height - 20);
        //egret.log(scaleBtn.width + "," + scaleBtn.height);
        scaleBtn.scale9Grid = rect;
        this.addChild(scaleBtn);

        var btn: egret.Bitmap = new egret.Bitmap();
        btn.texture = scaleBtn.texture;
        btn.x = scaleBtn.width;
        this.addChild(btn);
        //纹理集的使用
        var sp1: egret.Bitmap = new egret.Bitmap();
        sp1.texture = RES.getRes("animal.sprite1");
        sp1.x = 0;
        sp1.y = 150;
        this.addChild(sp1);

        var sp2: egret.Bitmap = new egret.Bitmap();
        var spritesheet: egret.SpriteSheet = RES.getRes("animal");
        sp2.texture =spritesheet.getTexture("sprite2");
        sp2.x = 150;
        sp2.y = 150;
        this.addChild(sp2);

        //截图功能
        var renderTexture: egret.RenderTexture = new egret.RenderTexture();
        renderTexture.drawToTexture(sky);
        renderTexture.toDataURL("image/png", new egret.Rectangle(0, 0, 200, 200));
        renderTexture.saveToFile("image/png", "capture.png", new egret.Rectangle(0, 0, 200, 200));

        this.bigText = new egret.TextField();
        this.bigText.text = "平移和滚动显示对象，Egret 一个开源的html5游戏引擎，基于typescript语言";
        this.bigText.scrollRect = new egret.Rectangle(0, 0, stageSize.x, 500);
        this.bigText.x = stageSize.x/2;
        this.bigText.y =300;
        this.bigText.width = 400;
        this.bigText.anchorOffsetX = this.bigText.width / 2;//必须在width设置之后设置
        this.bigText.cacheAsBitmap = true;   //位图缓存
        this.addChild(this.bigText);

        this.leftBtn = new egret.Shape();
        this.leftBtn.graphics.beginFill(0xff00ff);
        this.leftBtn.graphics.drawRect(0, 0, 50, 30);
        this.leftBtn.graphics.endFill();
        this.leftBtn.x = 100;
        this.leftBtn.y = 600;
        this.leftBtn.touchEnabled = true;
        this.addChild(this.leftBtn);
        this.leftBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onScroll, this);


        this.rightBtn = new egret.Shape();
        this.rightBtn.graphics.beginFill(0xffff00);
        this.rightBtn.graphics.drawRect(0, 0, 50, 30);
        this.rightBtn.graphics.endFill();
        this.rightBtn.x = 250;
        this.rightBtn.y = 600;
        this.rightBtn.touchEnabled = true;
        this.addChild(this.rightBtn);
        this.rightBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onScroll, this);

        
    }



    private onScroll(event: egret.TouchEvent): void {
        var rect: egret.Rectangle = this.bigText.scrollRect;
        switch (event.currentTarget) {
            case this.leftBtn:
                rect.x += 20;
                break;
            case this.rightBtn:
                rect.x -= 20;
                break;
        }
        this.bigText.scrollRect = rect;
    }

}